package com.kyzh.core.pager.weal.vip

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Html
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.gushenge.core.beans.HuiYuanBean
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.UserRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.databinding.ActivityOpenvipBinding
import com.kyzh.core.databinding.BottomdialogMoneycardBinding
import com.kyzh.core.databinding.ItemVipGoodBinding
import com.kyzh.core.databinding.ItemVipShuomingBinding
import com.kyzh.core.utils.setShader
import com.kyzh.core.utils.startActivity

class OpenVipActivity : BaseActivity(){
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, OpenVipActivity::class.java)
            context.startActivity(starter)
        }
    }
    private val goodAdapter  = GoodAdapter()
    private val shuoMingAdapter  = ShuoMingAdapter()
    private lateinit var db : ActivityOpenvipBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = DataBindingUtil.setContentView(this, R.layout.activity_openvip)
        db.proxy = Proxy()
        initView()
        initData()
    }

    private fun initView() {

        db.revGood.layoutManager = LinearLayoutManager(this,RecyclerView.HORIZONTAL,false)
        db.revGood.adapter = goodAdapter
        db.revShuoMing.adapter = shuoMingAdapter
        goodAdapter.setOnItemClickListener{a ,v ,p->
          val data =   a.data  as ArrayList<HuiYuanBean.Good>
            data.forEach {
                it.select = false
            }
            data[p].select = true
            goodAdapter.notifyDataSetChanged()
        }
    }

    fun initData(){
        UserRequest.getHuiYuan{
            db.data = this
            db.tvName.post {
                db.tvName.setShader(this.top.color)
            }
            goodAdapter.setNewInstance(this.goods)
            shuoMingAdapter.setNewInstance(this.shuoming)
            db.titleView.setOnMoreOnClickListener {
               AlertDialog.Builder(this@OpenVipActivity, R.style.kyzhGuestLoginDialog)
                    .setMessage(Html.fromHtml(jieshao)).setTitle("开通须知")
                    .setPositiveButton("关闭") { a, _ ->
                        a.dismiss()
                    }
                    .create().show()
            }
        }
    }


    private  inner  class  GoodAdapter:BaseQuickAdapter<HuiYuanBean.Good,BaseDataBindingHolder<ItemVipGoodBinding>>(R.layout.item_vip_good){
        override fun convert(
            holder: BaseDataBindingHolder<ItemVipGoodBinding>,
            item: HuiYuanBean.Good
        ) {
            holder.dataBinding?.data = item
        }
    }

    private inner class ShuoMingAdapter:BaseQuickAdapter<HuiYuanBean.Shuoming,BaseDataBindingHolder<ItemVipShuomingBinding>>(R.layout.item_vip_shuoming) {
        override fun convert(
            holder: BaseDataBindingHolder<ItemVipShuomingBinding>,
            item: HuiYuanBean.Shuoming
        ) {
            holder.dataBinding?.data = item
        }
    }
    
    inner class Proxy{
        fun openVip(){
            var id = ""
            goodAdapter.data.forEach {
                if(it.select) {
                    id =   it.id
                }
            }
            if(id.isNullOrBlank()){
                "请选择您要购买的会员套餐".toast()
                return
            }
            val inflate = BottomdialogMoneycardBinding.inflate(layoutInflater)
            val bottomSheetDialog = BottomSheetDialog(this@OpenVipActivity,R.style.BottomSheetDialogStyle)

            inflate.tvAli.setOnClickListener {
                 UserRequest.openHuiYuan(id,"1"){
                     this?.apply {
                         bottomSheetDialog.dismiss()
                         startActivity<BrowserActivity>(GlobalKeys.TITLE to "支付", GlobalKeys.LINK to this)
                    }
                }
            }
            inflate.tvWX.setOnClickListener {
                UserRequest.openHuiYuan(id,"2"){
                    this?.apply {
                        bottomSheetDialog.dismiss()
                        startActivity<BrowserActivity>(GlobalKeys.TITLE to "支付", GlobalKeys.LINK to this)
                    }
                }
            }
            inflate.cancel.setOnClickListener {
                bottomSheetDialog.dismiss()
            }
            bottomSheetDialog.setContentView(inflate.root)
            bottomSheetDialog.show()
        }
        fun goVip(){
            VipCentreActivity.start(this@OpenVipActivity)
            finish()
        }
    }

}